#include "GameOverState.h"
#include "Application.h"
#include "Fonts.h"
#include "Utils.h"
#include "Params.h"
#include "Goal.h"
#include <sfml/Graphics.hpp>

GameOverState::GameOverState()
{
	m_cNormal = sf::Color::White;
	m_cSelected = sf::Color::Yellow;
	m_cShadow = sf::Color::Color(50,50,50);

	sf::Font* font = Fonts::Istance()->GetFont("Consola");
	m_message = sf::Text("Game Over", (*font), 58);
	m_winner = sf::Text("NONE", (*font), 46);
	m_score = sf::Text("NONE", (*font), 46);
	m_menu[0] = sf::Text("Play Again", (*font), 26);
	m_menu[1] = sf::Text("Back to Main Menu", (*font), 26);

	m_shadows[0] = sf::Text("Play Again", (*font), 26);
	m_shadows[1] = sf::Text("Back to Main Menu", (*font), 26);

	m_spaceHeight = 0.f;
	for (int i  = 0; i < G_O_Elements ; i++)
	{
		m_spaceHeight = (m_menu[i].GetRect().Height>m_spaceHeight) ? m_menu[i].GetRect().Height : m_spaceHeight;
	}

	m_message.SetPosition(sf::Vector2f(
		(Application::Istance()->Window()->GetWidth()*0.5f ) - m_message.GetRect().Width*0.5f, 
		Application::Istance()->Window()->GetHeight()*0.5f 
			- (m_message.GetRect().Height + m_winner.GetRect().Height + m_score.GetRect().Height + m_spaceHeight*2)));
	m_message.SetColor(m_cNormal);

	m_posMenu = sf::Vector2f((Application::Istance()->Window()->GetWidth()*0.5f ), 
								Application::Istance()->Window()->GetHeight()*0.5f + m_spaceHeight*2);
	
	
	for (int i  = 0; i < G_O_Elements ; i++)
	{
		m_menu[i].SetPosition(sf::Vector2f(m_posMenu.x - m_menu[i].GetRect().Width*0.5f, 
											m_posMenu.y + m_spaceHeight*i));
		m_shadows[i].SetPosition(sf::Vector2f(m_menu[i].GetPosition().x + 2.f, m_menu[i].GetPosition().y + 2.f));
		m_shadows[i].SetColor(m_cShadow);
	}

	m_selected = 0;
	m_initialized = false;
	m_gameState = NULL;
}

GameOverState::~GameOverState()
{
}

void GameOverState::OnDraw(sf::RenderWindow& renderWindow)
{
	if (m_gameState != NULL)
	{
		m_gameState->GetPitchModel()->Draw(sf::Vector2f(0.f,0.f), sf::Vector2f(0.f,1.f));
	}

	renderWindow.SaveGLStates();
	
	renderWindow.Draw(m_message);
	renderWindow.Draw(m_winner);
	renderWindow.Draw(m_score);

	for (int i  = 0; i < G_O_Elements ; i++)
	{
		renderWindow.Draw(m_shadows[i]);
		m_menu[i].SetColor( ( i == m_selected) ? m_cSelected : m_cNormal );
		renderWindow.Draw(m_menu[i]);
	}

	renderWindow.RestoreGLStates();
}

void GameOverState::OnIdle(const sf::Input& input)
{
	if(!m_initialized)
	{
		m_initialized = true;

		m_gameState = static_cast<GameState*>(Application::Istance()->GetState("Game"));
		m_gameState->SetPaused();

		int redsScore = m_gameState->BlueGoal()->GetScore();
		int bluesScore = m_gameState->RedGoal()->GetScore();
		m_score.SetString((NumberToString(redsScore) + " - " + NumberToString(bluesScore)));

		if(redsScore > bluesScore)
		{
			m_winner.SetString("Reds WIN!");
			m_winner.SetColor(sf::Color::Red);
		}
		else if (redsScore < bluesScore)
		{
			m_winner.SetString("Blues WIN!");
			m_winner.SetColor(sf::Color::Blue);
		}
		else
		{
			m_winner.SetString("TIE!!");
			m_winner.SetColor(sf::Color::White);
		}

		m_winner.SetPosition(sf::Vector2f(
			(Application::Istance()->Window()->GetWidth()*0.5f ) - m_winner.GetRect().Width*0.5f, 
			Application::Istance()->Window()->GetHeight()*0.5f 
				- (m_winner.GetRect().Height + m_spaceHeight + m_score.GetRect().Height)));
		m_score.SetPosition(sf::Vector2f(
			(Application::Istance()->Window()->GetWidth()*0.5f ) - m_score.GetRect().Width*0.5f, 
			Application::Istance()->Window()->GetHeight()*0.5f - (m_score.GetRect().Height)));

	}

	if( Application::Istance()->IsInputAvailable() )
	{
		if (input.IsKeyDown(sf::Key::Return) || input.IsJoystickButtonDown(0,2) )
		{
			switch (m_selected)
			{
				case 0:
					{
						m_initialized = false;
						Application::Istance()->SetState("Game");
					}
					break;
				case 1:
					{
						m_initialized = false;
						Application::Istance()->SetState("Main");
						m_selected = 0;
					}
					break;
				default:
					break;
			}
			Application::Istance()->PauseInput(0.2f);
		}
		else if (input.IsKeyDown(sf::Key::Down) || input.GetJoystickAxis(0,sf::Joy::AxisY)>=50.f )
		{
			m_selected = (m_selected == G_O_Elements-1) ? 0 : m_selected + 1 ;
			Application::Istance()->PauseInput(0.2f);
		}
		else if (input.IsKeyDown(sf::Key::Up) || input.GetJoystickAxis(0,sf::Joy::AxisY)<=-50.f )
		{
			m_selected = (m_selected == 0) ? G_O_Elements-1 : m_selected - 1 ;
			Application::Istance()->PauseInput(0.2f);
		}
			
	}

}


